import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/login/Login.vue"
import Home from '@/views/home/Home.vue'
import Welcome from '@/views/welcome/Welcome.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    // component: import('../views/home/Home.vue')
    component: Home,
    redirect: '/welcome',
    children:[
      {path: '/welcome',name:'welcome',component: Welcome},
      {path: '/users',name: 'users', component:()=>import('@/views/users/Users.vue')},
      {path: '/roles',name: 'roles', component:()=>import('@/views/roles/Roles.vue')},
      {path: '/rights',name: 'rights', component:()=>import('@/views/rights/Rights.vue')},
      {path: '/goods',name: 'goods', component:()=>import('@/views/goods/Goods.vue')},
      {path: '/goods/add',name: 'addGoods', component:()=>import('@/views/goods/add/AddGoods.vue')},
      {path: '/params',name: 'params', component:()=>import('@/views/params/Params.vue')},
      {path: '/categories',name: 'categories',component:()=>import('@/views/categories/Categories.vue')},
      {path: '/orders',name: 'orders',component:()=>import('@/views/orders/Orders.vue')},
      {path: '/reports',name: 'reports',component:()=>import('@/views/reports/Reports.vue')},
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  let token = window.localStorage.getItem('myToken');
  if(token) return next();
  if(!token) to.path=="/login"? next():next({path: '/login'});
})



export default router
